package RTL.Task2;

//编程获取两个指定字符串中的最大相同子串

public class MaxString {

    public static void main(String[] args) {
        String s1 = "gduaaad2fe";
        String s2 = "frehgdaaafuyvewvfbuhbfd";
        String maxStr = getMaxSubString(s1, s2);
        System.out.println("最大相同子串是：" + maxStr);
    }

    public static String getMaxSubString(String s1, String s2) {
        //初始化最长最短字符串，再声明两个字符串的长度大小
        String max = null;
        String min = null;
        if (s1.length()>s2.length()) {
            max=s1;
            min=s2;
        }else {
            max=s2;
            min=s1;
        }
        //依次把短的字符串的长度递减，找出短的字符串的子字符串
        for (int x = 0; x < min.length(); x++) {
            for (int y = 0, z = min.length() - x; z != min.length() + 1; y++, z++) {
                String temp = min.substring(y, z);
                if (max.contains(temp)) {
                    return temp;
                }
            }
        }
        return null;
    }
}


